﻿
Imports System.Xml.Serialization

Namespace Utils

    Public MustInherit Class DataUtils


        Public Shared Function SerializeToXmlString(ByVal toSerialize As Object) As String

            Dim serializer As New XmlSerializer(toSerialize.GetType())
            Dim ms As New System.IO.MemoryStream

            serializer.Serialize(ms, toSerialize)

            Return System.Text.Encoding.UTF8.GetString(ms.ToArray())

        End Function


        Public Shared Function DeserializeFromXmlString(ByVal toDeserialize As String, ByVal type As Type) As Object

            Dim deserializer As New XmlSerializer(type)
            Dim ms As New IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(toDeserialize))

            Return deserializer.Deserialize(ms)

        End Function

        Public Shared Function DbNull2Nothing(Of T)(ByVal value As Object) As T
            If TypeOf value Is System.DBNull Then
                Return Nothing
            Else
                Return CType(value, T)
            End If
        End Function


    End Class

End Namespace